using System;
using Microsoft.SPOT;

namespace Gadgeteer.Modules.Community.XBee
{
    /// <summary>
    /// Parses the API message for TransmitStatus (when you send a message, you may get a status message)
    /// </summary>
    public class TransmitStatus : BaseAPIMessage
    {
        /// <summary>
        /// 16 bit address packet was delivered to
        /// </summary>
        public ushort DestinationAddress { get; private set; }
        /// <summary>
        /// how many attempts it took
        /// </summary>
        public ushort TransmitRetryCount { get; private set; }
        /// <summary>
        /// delivery status : TODO: parse this into an enum
        /// </summary>
        public ushort DeliveryStatus { get; private set; }
        /// <summary>
        /// discovery status
        /// </summary>
        public ushort DiscoveryStatus { get; private set; }

        /// <summary>
        /// APIMessageEnum.TransmitStatus
        /// </summary>
        protected override ushort MessageType
        {
            get { return (ushort)APIMessageEnum.TransmitStatus; }
        }

        /// <summary>
        /// parses the message body
        /// </summary>
        /// <param name="message">API Message</param>
        public override void ParseMessageBody(byte[] message)
        {
            // 16-bit address
            // destination
            // 0x7D If successful, this is the 16-bit network address the packet was
            //  delivered to. If not successful, this address matches the destination network
            // address that was provided in the Transmit Request frame.
            DestinationAddress = (ushort)((message[5] << 8) + message[6]);

            // Transmit retry count: The number of application transmission retries that took place.
            TransmitRetryCount = (ushort)message[7];

            // Delivery Status
            DeliveryStatus = (ushort)message[8];

            // Discovery Status
            DiscoveryStatus = (ushort)message[9];
        }

        /// <summary>
        /// formatted string of the message body
        /// </summary>
        /// <returns>formatted string of the message body</returns>
        public override string MessageBodyToString()
        {
            // TODO: format delivery and discovery status
            return "16 bit address destination: " + DestinationAddress.ToString() + "\n"
                 + "Transmit Retry Count: " + TransmitRetryCount.ToString() + "\n"
                 + "Delivery Status: " + DeliveryStatus.ToString() + "\n"
                 + "Discovery Status: " + DiscoveryStatus.ToString() + "\n";
        }

        /// <summary>
        /// APIMessageEnum.TransmitStatus.ToString()
        /// </summary>
        /// <returns>APIMessageEnum.TransmitStatus.ToString()</returns>
        protected override string MessageTypeToString()
        {
            return APIMessageEnum.TransmitStatus.ToString();
        }
    }
}
